package org.dahuatech.ssh.service.organization;

import java.util.List;

import org.dahuatech.ssh.common.utils.encode.JsonBinder;
import org.dahuatech.ssh.dao.organization.OrganizationDao;
import org.dahuatech.ssh.entity.organization.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OrganizationManager {
	private OrganizationDao organizationDao;

	@Autowired
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}
	
	/**
	 * 获取所有部门
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Organization> listAllOrganization(){
		return organizationDao.getAll();
	}
	
	@Transactional(readOnly=true)
	public String listAllOrganizationJson(){
		List<Organization> list=listAllOrganization();
		for(Organization organization:list){
			organization.setIcon("/ssh/include/images/shared/org.gif");
			if("001".equals(organization.getCode())){
				organization.setOpen(true);
			}
		}
		return JsonBinder.buildNonDefaultBinder().toJson(list);
	}
	
	/**
	 * 按ID获取部门
	 * @param id
	 * @return
	 */
	@Transactional(readOnly=true)
	public Organization getOrganization(Long id){
		return organizationDao.get(id);
	}
	
	/**
	 * 获取下级部门
	 * @param pid
	 * @return
	 */
	@Transactional(readOnly=true)
	public List<Organization> listAllSubOrganization(Long pid){
		return organizationDao.findBy("pId", pid);
	}
	
	@Transactional(readOnly=true)
	public String listAllSubOrganizationJson(Long pid){
		return JsonBinder.buildNonDefaultBinder().toJson(listAllSubOrganization(pid));
	}
	
	@Transactional(readOnly=true)
	public String getOrganizationSubCode(String code){
		return organizationDao.getOrganizationSubCode(code);
	}
	
	
	/**
	 * 删除部门
	 * @param id
	 */
	public void deleteOrganiztion(Long id){
		organizationDao.delete(id);
	}
	
	/**
	 * 保存或者更新部门
	 * @param entity
	 */
	public void saveOrganiztion(Organization entity){
		organizationDao.save(entity);
	}

	public OrganizationDao getOrganizationDao() {
		return organizationDao;
	}
	
}
